<template>
  <div class="dashboard-container">
    <div class="app-container" />
    <!-- 组织架构的内容 - 头部 -->
    <el-card class="tree-card">
      <!-- 放置结构内容 -->
      <!-- 组织架构树 -->
      <tree-tools :tree-node="company" :isroot="true" @addDepts="addDepts" />
      <el-tree :data="departs" :default-expand-all="true" :props="defaultProps">
        <!-- 传入内容 插槽内容 会循环多次-->
        <!-- 作用域插槽 slot-scope 传递给插槽的数据 -->
        <tree-tools slot-scope="{ data }" :tree-node="data" @editDepts="editDepts" @delDepts="getDepartments" @addDepts="addDepts" />
      </el-tree>
    </el-card>
    <!-- 放置弹层组件 -->
    <addDept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import treeTools from './components/tree-tools'
import addDept from './components/add-dept'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
export default {
  components: {
    treeTools,
    addDept
  },
  data() {
    return {
      // 头部的数据
      company: {
        name: '',
        manager: '负责人',
        id: ''
      },
      departs: [],
      defaultProps: {
        label: 'name'
      },
      showDialog: false, // 弹层显示
      node: null // 记录当前点击的node节点
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const res = await getDepartments()
      // 头部名称
      this.company.name = res.companyName
      this.departs = tranListToTreeData(res.depts, '') // 转化树形结构
    },
    // 监听添加子部门的事件
    // node 当前点击的部门
    addDepts(node) {
      this.showDialog = true // 开启弹出层
      this.node = node
    },
    // 监听编辑的按钮
    editDepts(node) {
      this.showDialog = true
      this.node = node
      // 调用子组件的方法
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
